<template>
    <div class="login-container">
  <div class="login-form-wrap">
    <div class="login-head">
      <div class="logo"></div>
    </div>
    <!--element-ui  表单组件 -->
    <el-form class="login-form"
              :model="form"
              :rules="rules"
              ref="loginForm"
     >
      <el-form-item prop="mobile">
        <el-input
          placeholder="请输入手机号"
          v-model="form.mobile"
        ></el-input>
      </el-form-item>
      <el-form-item prop="code">
        <el-input
          placeholder="请输入验证码"
          v-model="form.code"
        ></el-input>
      </el-form-item>
      <el-form-item prop="loginFlag" >
        <el-checkbox v-model="form.loginFlag">我已阅读并同意用户协议和隐私条款</el-checkbox>
      </el-form-item>
      <el-form-item>
        <el-button
          class="login-btn"
          type="primary"
          @click="login"
        >登录</el-button>
      </el-form-item>
    </el-form>
  </div>
  </div>
</template>
<script>

import { login as hlogin } from '@/api/user'
import { setUserInfo as setInfo } from '@/store'

export default {
  name: 'LoginIndex',
  data () {
    const validateFlag = (rule, value, callback) => {
      // value:需要校验的表单项当前的输入值
      // callback: 可执行的函数 在校验通过或者不同都需要调用这个函数通知验证消息
      // loginFlag 如果为true则通过 如果为false则不过
      // 注意:new Error里面的文字会被当成错误信息提示给用户 验证通过后记者还需要调用callback
      if (!value) {
        callback(new Error('请先勾选协议'))
      } else {
        callback()
      }
    }
    return {
      form: {
        mobile: '13911111111', // 手机号
        code: '246810', // 验证码
        loginFlag: false
      },
      rules: {
        mobile: [
          { required: true, message: '请输入手机号', trigger: 'blur' },
          { min: 3, max: 11, message: '请输入11位的手机号', trigger: 'blur' }
        ],
        code: [
          { required: true, message: '请输入验证码', trigger: 'blur' }
        ],
        loginFlag: [
          { validator: validateFlag, trigger: 'change' }
        ]
      }
    }
  },
  methods: {
    login () {
      this.$refs.loginForm.validate((valid) => {
        // valid为布尔值:当表单所有需要验证的表单都通过才为true 否则为false
        // 只有调用validate方法会对所有需要验证的表单重新走一遍校验逻辑'兜底'
        if (valid) {
          hlogin(this.form.mobile, this.form.code)
            .then(res => {
              setInfo(res.data.data)
              this.$router.push({
                path: '/'
              })
            }
            )
        } else {
          return false
        }
      })
    }
  }

}
</script>

<style lang="less" scoped>
.login-container {
  position: fixed;
  left: 0;
  top: 0;
  right: 0;
  bottom: 0;
  display: flex;
  flex-direction:column;
  justify-content: center;
  align-items: center;
  background: url("./img/login_bg.jpg") no-repeat;
  background-size: cover;
  .login-form-wrap {
    min-width: 300px;
    padding: 30px 50px 10px;
    background-color: #fff;
    .login-head {
      display: flex;
      justify-content: center;
      .logo {
        width: 200px;
        height: 57px;
        background: url("./img/logo_index.png") no-repeat;
        background-size: contain;
      }
    }
    .login-form {
      .login-btn {
        width: 100%;
      }
    }
  }
}
</style>
